package com.dl.designmodel.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @ProjectName: mcroservices
 * @Package: com.dl.designmodel.proxy
 * @ClassName: BeanFactory
 * @Author: 86977
 * @Description: ${description}
 * @Date: 2021/6/30 17:11
 * @Version: 1.0
 */
public class BeanFactory {
  public static BaseService newInstance(Class clazz){
//    创建蝉真实对象
    BaseService trueCicada=new Cicada();
    //创建代理类 螳螂
    InvocationHandler prayingMaints=new PrayingMantis(trueCicada);
    Class[] clazzArray={BaseService.class};
    BaseService baseService=(BaseService) Proxy.newProxyInstance(clazz.getClassLoader(),clazzArray,prayingMaints);
    return baseService;
  }

  public static BaseService newInstance1(Class clazz){
//    创建蝉真实对象
    BaseService trueCicada=new Cicada();
    //创建代理类 螳螂
    InvocationHandler prayingMaints=new PrayingMantis(trueCicada);
    Class[] clazzArray={BaseService.class};
    BaseService baseService=(BaseService) Proxy.newProxyInstance(clazz.getClassLoader(),clazzArray,prayingMaints);

   //
    InvocationHandler cardinal= new Cardinal(baseService);

    BaseService secondBaseService=(BaseService) Proxy.newProxyInstance(clazz.getClassLoader(),clazzArray,cardinal);


    return secondBaseService;
  }

}
